package com.hejia.alauda.service;

import java.util.Date;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hejia.alauda.entity.SplashScreen;
import com.hejia.alauda.entity.SplashScreenImage;
import com.hejia.alauda.entity.SplashScreenSchedule;
import com.hejia.alauda.entity.example.SplashScreenScheduleExample;
import com.hejia.alauda.persistence.mybatis.SplashScreenImageMapper;
import com.hejia.alauda.persistence.mybatis.SplashScreenScheduleMapper;
import com.hejia.alauda.utils.Pager;

@Service
public class SplashScreenServiceImpl implements SplashScreenService {

	@Resource
	private SplashScreenScheduleMapper splashScreenScheduleMapper;
	
	@Resource
	private SplashScreenImageMapper splashScreenImageMapper;
	
	@Override
	public Pager<SplashScreenSchedule> findSplashScreenSchedules(
			boolean isShow, String description, Pager<SplashScreenSchedule> pager) {
		
		SplashScreenScheduleExample example = new SplashScreenScheduleExample();
		
		if (isShow) {
			example.appendCriterion("endTime > now()");
		} else {
			example.appendCriterion("endTime < now()");
		}
		
		if (StringUtils.isNotEmpty(description)) {
			example.appendCriterion("description like", "%" + description + "%");
		}
		
		example.setOrderByClause("priority desc");
		
		pager.setList(this.splashScreenScheduleMapper.selectByExample(example, pager));
		pager.setTotalCount(this.splashScreenScheduleMapper.countByExample(example));
		
		return pager;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void addSplashScreenSchedule(SplashScreenSchedule schedule) {
		
		schedule.setCreateDate(new Date());
		schedule.setModifyDate(new Date());
		if (this.splashScreenScheduleMapper.insert(schedule) < 1) {
			throw new RuntimeException("插入启动画面排期错误");
		}
		
		for(SplashScreenImage image : schedule.getImages()) {
			image.setSchedule(schedule);
			image.setCreateDate(new Date());
			image.setModifyDate(new Date());
			image.setFileName(UUID.randomUUID() + ".png");
			if (this.splashScreenImageMapper.insert(image) < 1) {
				throw new RuntimeException("插入启动画面图片错误");
			}
		}
	}

	@Override
	public SplashScreenSchedule findSplashScreenScheduleById(Long id) {
		
		return this.splashScreenScheduleMapper.selectByPrimaryKey(id);
	}

	@Override
	public void updateSplashScreenSchedule(SplashScreenSchedule schedule) {
		
		SplashScreenSchedule s = this.splashScreenScheduleMapper.selectByPrimaryKey(schedule.getId());
		
		s.setModifyDate(new Date());
		s.setDevice(schedule.getDevice());
		s.setStartTime(schedule.getStartTime());
		s.setEndTime(schedule.getEndTime());
		s.setPriority(schedule.getPriority());
		s.setDescription(schedule.getDescription());
		
		if (this.splashScreenScheduleMapper.updateByPrimaryKey(s) < 1) {
			throw new RuntimeException("更新启动画面排期错误");
		}
		
		for(SplashScreenImage image : schedule.getImages()) {
			SplashScreenImage i = this.splashScreenImageMapper.selectByPrimaryKey(image.getId());
			if (!i.getUrl().equals(image.getUrl())) {
				i.setFileName(UUID.randomUUID() + ".png");
			}
			i.setSchedule(s);
			i.setWidth(image.getWidth());
			i.setHeight(image.getHeight());
			i.setDpi(image.getDpi());
			i.setUrl(image.getUrl());
			i.setModifyDate(new Date());
			
			if (this.splashScreenImageMapper.updateByPrimaryKey(i) < 1) {
				throw new RuntimeException("更新启动画面图片错误");
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteSplashScreenScheduleById(Long id) {
		
		SplashScreenSchedule schedule = this.splashScreenScheduleMapper.selectByPrimaryKey(id);
		if (this.splashScreenScheduleMapper.deleteByPrimaryKey(schedule) < 1) {
			throw new RuntimeException("删除启动画面排期错误");
		}
		
		for (SplashScreenImage image : schedule.getImages()) {
			if (this.splashScreenImageMapper.deleteByPrimaryKey(image) < 1) {
				throw new RuntimeException("删除启动画面图片错误");
			}
		}
	}

	@Override
	public SplashScreen findSplashScreen(String device, Integer width, Integer height, Integer dpi) {
		
		SplashScreenImage image = this.splashScreenImageMapper
				.selectSplashScreenImage(device, width, height, dpi);
		if(image == null){
			return null;
		} else {
			SplashScreen splashScreen = new SplashScreen();
			 
			splashScreen.setOs(image.getSchedule().getDevice().toString());
			splashScreen.setStartTime(image.getSchedule().getStartTime());
			splashScreen.setEndTime(image.getSchedule().getEndTime());
			splashScreen.setFileName(image.getFileName());
			splashScreen.setUrl(image.getUrl());
			splashScreen.setDpi(image.getDpi());
			splashScreen.setWidth(image.getWidth());
			splashScreen.setHeight(image.getHeight());
			
			return splashScreen;
		}
	}
}
